package com.tgpms.web.earlierStage.service.impl;

import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.util.Common;
import com.tgpms.web.earlierStage.dao.DisputeItemsMapper;
import com.tgpms.web.earlierStage.entity.DisputeItems;
import com.tgpms.web.earlierStage.service.EarlierDisItemService;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class EarlierDisItemServiceImpl implements EarlierDisItemService {
	
	@Autowired
	private DisputeItemsMapper disputeItemsMapper;

	@Override
	public Result addDisItem(DisputeItems disItem, Result result) {
		//查询唯一键是否重复
//		if(disputeItemsMapper.selByUniqueKey(disItem.getTaskId(),disItem.getDisputeId())!=null) {
//			result.setSuccess(false);
//			result.setMsg(ExceptionExplain.ERROR_BY_INSERT_REPETITION.getExplain());
//			return result;
//		}
		//主键形式 yyyyMMddHHmmss+UUID
		disItem.setId(Common.fromDateFormats()+UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
		disItem.setDisputeId(disItem.getId());
		disItem.setRowstamp(disItem.getId());
		disItem.setReportDate(new Date());
		disItem.setInsertDate(new Date());
		int i = disputeItemsMapper.insertSelective(disItem);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
			result.setData(disItem.getId());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}

	@Override
	public Result updateDisItem(DisputeItems disItem, Result result) {
		disItem.setModifyDate(new Date());
		int i = disputeItemsMapper.updateByPrimaryKeySelective(disItem);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}

	@Override
	public Result deleteDisItem(String id, Result result) {
		int i = disputeItemsMapper.deleteByPrimaryKey(id);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}

}
